//
//  SSCMoreGameUnityAdapter.m
//  Unity-iPhone
//
//  Created by xuzepei on 16/1/19.
//
//

#import "SSCInternalSDKManagerAdapter.h"
#if __has_include(<SSCAdsSDK/SSCAdsSDK.h>)
#import <SSCAdsSDK/SSCAdsSDK.h>
#else
#import "AdsSdk.h"
#import "AdIdHelper.h"
#endif
#import "SSCMoreGameManagerAdapter.h"
#import "SSCShareUnityAdapter.h"
#import "SSCNewsBlastAdapter.h"
#import "SSCRateToolAdapter.h"


@implementation SSCInternalSDKManagerAdapter

+ (SSCInternalSDKManagerAdapter*)getInstance
{
    static SSCInternalSDKManagerAdapter* sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token, ^{
        sharedInstance = [[SSCInternalSDKManagerAdapter alloc] init];
        [SSCInternalSDK getInstance].delegate = sharedInstance;
    });
    
    return sharedInstance;
}

- (void)setGameObjectName:(char*)name
{
    if(name && strlen(name))
    {
        self.objectName = [NSString stringWithFormat:@"%s",name];
    }
}

- (void)popAlertDialog:(char*)message
{
    if(message && strlen(message))
    {
        [[SSCInternalSDK getInstance] showAlert:@"" message:[NSString stringWithFormat:@"%s",message]];
    }
}

- (bool)checkNetworkAvailable
{
    return [[SSCInternalSDK getInstance] reachabilityForInternetConnection];
}

- (bool)isTablet
{
    return [[SSCInternalSDK getInstance] isPad];
}

- (void)showMoreGame
{
    if([self.objectName length])
    {
       [[SSCMoreGameManagerAdapter getInstance] setObjectName:self.objectName];
    }
    
    [[SSCMoreGameManagerAdapter getInstance] show:1];
    //[[SSCInternalSDK getInstance] showMoreGame];
}

- (void)doNewsBlast
{
    if([self.objectName length])
    {
        [[SSCNewsBlastAdapter getInstance] setObjectName:self.objectName];
    }
    
    [[SSCNewsBlastAdapter getInstance] startNews:NO];
    //[[SSCInternalSDK getInstance] startNewsBlast];
}

- (void)saveToAlbum:(UIImage*)image
{
//    if(image)
//    {
//        NSString* imagePath = [NSString stringWithFormat:@"%@/image_will_save_to_album",NSTemporaryDirectory()];
//        NSData *data = UIImageJPEGRepresentation(image, 1.0);
//        if(data)
//            [data writeToFile:imagePath atomically:YES];
//        
//        [[SSCInternalSDK getInstance] saveToAlbum:imagePath];
//    }
    
    if([self.objectName length])
    {
        [[SSCShareUnityAdapter getInstance] setObjectName:self.objectName];
    }
    
    [[SSCShareUnityAdapter getInstance] saveToPhotoLibrary:image];
}

- (void)rateUs
{
    if([self.objectName length])
    {
        [[SSCRateToolAdapter getInstance] setObjectName:self.objectName];
    }
    
    NSDictionary* configDic=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AppConfig.plist" ofType:nil]];
    if(configDic && [configDic isKindOfClass:[NSDictionary class]])
    {
        NSString* appleID = [configDic objectForKey:@"AppleId"];
        if([appleID length])
        {
            [[SSCRateToolAdapter getInstance] rateForAppleId:(char *)appleID.UTF8String alertMessage:nil];
        }
        else
        {
            NSLog(@"AppleId cannot be found in AppConfig.plist file!");
        }
    }
}

- (void)sendEmail:(char*)subject message:(char*)message filePath:(char*)filePath
{
//    NSString* para0 = nil;
//    NSString* para1 = nil;
//    NSString* para2 = nil;
//    
//    if(subject && strlen(subject))
//        para0 = [NSString stringWithFormat:@"%s",subject];
//    
//    if(message && strlen(message))
//        para1 = [NSString stringWithFormat:@"%s",message];
//    
//    if(filePath && strlen(filePath))
//        para2 = [NSString stringWithFormat:@"%s",filePath];
    
    if([self.objectName length])
    {
        [[SSCShareUnityAdapter getInstance] setObjectName:self.objectName];
    }
    
    [[SSCShareUnityAdapter getInstance] shareToEmail:subject message:message imagePath:filePath];
}

- (void)showLoadingView {
    
    [[AdsSdk getInstance] showLoadingView:nil];
}

- (void)removeLoadingView {
    
    [[AdsSdk getInstance] removeLoadingView];
}

#pragma mark - Delegate

- (const char*)getJsonString:(NSDictionary*)dictonary
{
    NSString *jsonString = nil;
    if(dictonary)
    {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictonary
                                                           options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                             error:&error];
        
        if (! jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        }
    }
    
    if([jsonString length])
    {
        return [jsonString UTF8String];
    }
    
    return "";
}

- (void)networkIsReady
{
    NSLog(@"%s",__FUNCTION__);
    static NSString *methodName = @"OnNetworkIsReady";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

- (void)shareSuccessed:(int)type
{
    static NSString *methodName = @"OnShareSuccessed";
    if([self.objectName length])
    {
        NSDictionary* dict = @{@"share_type":[NSString stringWithFormat:@"%d",type]};
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],[self getJsonString:dict]);
    }
}

- (void)shareFailed:(int)type errorMessage:(NSString*)errorMessage
{
    static NSString *methodName = @"OnShareFailed";
    if([self.objectName length])
    {
        if(0 == [errorMessage length])
            errorMessage = @"";
        
        NSDictionary* dict = @{@"share_type":[NSString stringWithFormat:@"%d",type],@"error":errorMessage};
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],[self getJsonString:dict]);
    }
}

@end

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
    
    //To set the name of game object for UnitySendMessage method
    void _setGameObjectNameForInternal(char* gameObjectName)
    {
        [[SSCInternalSDKManagerAdapter getInstance] setGameObjectName:gameObjectName];
    }
    
    void _popAlertDialog(char* message)
    {
        [[SSCInternalSDKManagerAdapter getInstance] popAlertDialog:message];
    }
    
    void _startToCheckNetwork()
    {
        [[SSCInternalSDK getInstance] returnNetworkStatus];
    }
    
    void _showNetworkAlert()
    {
        [[SSCInternalSDK getInstance] showNetworkAlert];
    }
    
    float _densityScale()
    {
        return [UIScreen mainScreen].scale;
    }
    
    bool _checkNetworkAvailable()
    {
        return [[SSCInternalSDKManagerAdapter getInstance] checkNetworkAvailable];
    }
    
    bool _isTablet()
    {
        return [[SSCInternalSDKManagerAdapter getInstance] isTablet];
    }
    
    void _showMoreGames()
    {
        [[SSCInternalSDKManagerAdapter getInstance] showMoreGame];
    }
    
    void _doNewsBlast()
    {
        [[SSCInternalSDKManagerAdapter getInstance] doNewsBlast];
    }
    
    void _saveToAlbum(const Byte* imageBytes, int dataLength)
    {
        NSData *data = [NSData dataWithBytes:imageBytes length:dataLength];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            if(image)
                [[SSCInternalSDKManagerAdapter getInstance] saveToAlbum:image];
        }
    }
    
    void _rateUs()
    {
        [[SSCInternalSDKManagerAdapter getInstance] rateUs];
    }
    
    void _sendEmail(char*subject, char* message)
    {
        [[SSCInternalSDKManagerAdapter getInstance] sendEmail:subject message:message filePath:nil];
    }
    
    void _sendEmailAndFilePic(char*subject, char* message, char* fileName)
    {
        [[SSCInternalSDKManagerAdapter getInstance] sendEmail:subject message:message filePath:fileName];
    }
    
    void _refreshDCIM(char* sFileDir)
    {
       NSLog(@"This method is only for Andorid.%s",__FUNCTION__);
    }
    
    char* _getSDCardPath(char* sFileDir)
    {
        NSLog(@"This method is only for Andorid.%s",__FUNCTION__);
        return (char*)[@"" UTF8String];
    }
    
    void _makeToast(char* message)
    {
        NSLog(@"This method is only for Andorid.%s",__FUNCTION__);
    }
    
    int _listAssetFiles(char* path)
    {
        NSLog(@"This method is only for Andorid.%s",__FUNCTION__);
        return 0;
    }
    
    void _endSession()
    {
        NSLog(@"This method is only for Andorid.%s",__FUNCTION__);
    }
    
    void _showLoadingView()
    {
        [[SSCInternalSDKManagerAdapter getInstance] showLoadingView];
    }
    
    void _removeLoadingView()
    {
        [[SSCInternalSDKManagerAdapter getInstance] removeLoadingView];
    }
    
}
